<script setup lang="tsx">
import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { fetchGetLoginLogList } from '@/service/api';
import { useAppStore } from '@/store/modules/app';
import { useBoolean } from '@sa/hooks';
import { computed, reactive, watch, ref } from 'vue';
import { useTable, useTableOperate } from '@/hooks/common/table';
import { $t } from '@/locales';
import LoginSearch from './modules/login-search.vue';

const appStore = useAppStore();
/** is super role in static route */
const { VITE_STATIC_SUPER_ROLE } = import.meta.env;

const {
  columns,
  columnChecks,
  data,
  loading,
  getData,
  getDataByPage,
  mobilePagination,
  searchParams,
  resetSearchParams
} = useTable({
  apiFn: fetchGetLoginLogList,
  apiParams: {
    current: 1,
    size: 10,
    username: null,
    loginType: null
  },
  columns: () => [
    {
      type: 'selection',
      align: 'center',
      width: 48,
      fixed: 'left'
    },
    {
      key: 'index',
      title: $t('common.index'),
      width: 64,
      align: 'center',
      fixed: 'left'
    },
    {
      key: 'username',
      title: '用户名',
      align: 'center',
      minWidth: 100,
      fixed: 'left'
    },
    {
      key: 'ipaddr',
      title: '登录地址',
      align: 'center',
      minWidth: 100
    },
    // {
    //   key: 'continent',
    //   title: '大洲',
    //   align: 'center',
    //   minWidth: 100
    // },
    {
      key: 'country',
      title: '国家',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'province',
      title: '省份',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'city',
      title: '城市',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'district',
      title: '区县',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'isp',
      title: '运营商',
      align: 'center',
      minWidth: 100
    },
    // {
    //   key: 'areaCode',
    //   title: '区号',
    //   align: 'center',
    //   minWidth: 100
    // },
    // {
    //   key: 'cityCode',
    //   title: '城市代码',
    //   align: 'center',
    //   minWidth: 100
    // },
    // {
    //   key: 'longitude',
    //   title: '经度',
    //   align: 'center',
    //   minWidth: 100
    // },
    // {
    //   key: 'latitude',
    //   title: '纬度',
    //   align: 'center',
    //   minWidth: 100
    // },
    {
      key: 'browser',
      title: '浏览器',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'os',
      title: '操作系统',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'agent',
      title: '代理',
      align: 'center',
      ellipsis: { tooltip: true },
      minWidth: 100
    },
    {
      key: 'message',
      title: '错误信息',
      align: 'center',
      ellipsis: { tooltip: true },
      minWidth: 100
    },
    {
      key: 'params',
      title: '参数',
      align: 'center',
      ellipsis: { tooltip: true },
      minWidth: 100
    },
    {
      key: 'createdAt',
      title: '登录时间',
      align: 'center',
      minWidth: 100
    },
    {
      key: 'loginType',
      title: '登录类型',
      align: 'center',
      minWidth: 100,
      fixed: 'right'
    }
  ]
});

const {
  handleEdit,
  checkedRowKeys,
  onBatchDeleted,
  onDeleted
  // closeDrawer
} = useTableOperate(data, getData);

async function handleBatchDelete() {
  // request
  // console.log(checkedRowKeys.value);
  // const { error } = await fetchDeleteResource(checkedRowKeys.value);
  // if (error) return;
  onBatchDeleted();
}

function handleDelete(id: number) {
  // request
  console.log(id);

  onDeleted();
}

</script>

<template>
  <div class="min-h-500px flex-col-stretch gap-16px overflow-hidden lt-sm:overflow-auto">
    <LoginSearch v-model:model="searchParams" @reset="resetSearchParams" @search="getDataByPage" />
    <NCard title="登录日志" :bordered="false" size="small" class="sm:flex-1-hidden card-wrapper">
      <template #header-extra>
        <TableHeaderOperation v-model:columns="columnChecks" :disabled-delete="checkedRowKeys.length === 0"
          :loading="loading" @refresh="getData">
          <template #prefix>
            <NPopconfirm @positive-click="handleBatchDelete">
              <template #trigger>
                <NButton size="small" ghost type="error" :disabled="checkedRowKeys.length === 0">
                  <template #icon>
                    <icon-ic-round-delete class="text-icon" />
                  </template>
                  {{ $t('common.batchDelete') }}
                </NButton>
              </template>
              {{ $t('common.confirmDelete') }}
            </NPopconfirm>
          </template>
        </TableHeaderOperation>
      </template>
      <NDataTable v-model:checked-row-keys="checkedRowKeys" :columns="columns" :data="data" size="small"
        :flex-height="!appStore.isMobile" :scroll-x="2000" :loading="loading" remote :row-key="row => row.id"
        :pagination="mobilePagination" class="sm:h-full" />
    </NCard>
  </div>
</template>

<style scoped></style>
